﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

using System.Text;
using System.IO;
using System.Web.Configuration;
using log4net;
namespace Recon.Portal.WebUI
{
    public class Global : System.Web.HttpApplication
    {
        protected static readonly ILog log = LogManager.GetLogger(typeof(Global));
        protected void Application_Start(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
            //log.Info("Application_Start() Called");
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            log.Info("Session_Start() Called Event :: "+e.ToString());
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            log.Info("Application_BeginRequest() Called Event :: " + e.ToString());
            string Url = Request.RawUrl;
            if (Url.Contains("ForgotPasswordPage.aspx"))
            {
                HttpContext.Current.SkipAuthorization = true;
            }
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            log.Info("Application_AuthenticateRequest() Called Event :: " + e.ToString());
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            log.Info("Application_Error() Called Event :: " + e.ToString());
            //fetch last exception and prepare string representing eception details
            Exception lastException = Server.GetLastError();
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Date: " + DateTime.Now.ToLongDateString());
            sb.AppendLine("Time: " + DateTime.Now.ToLongTimeString());
            if (lastException != null)
            {
                sb.AppendLine("Exception: " + lastException.Message);
                sb.AppendLine("Exception Type: " + lastException.GetType().FullName);
                if (lastException.InnerException != null)
                {
                    sb.AppendLine("Inner Exception: " + lastException.InnerException);
                }
                if (lastException.StackTrace != null)
                {
                    sb.AppendLine("Stack Trace: " + lastException.StackTrace);
                }
            }
            else
            {
                sb.AppendLine("Exception: Unknown Exception, possibly an error in navigation."+e.GetType());
            }

            //clear exception from Server object, log exception to log file and navigate to custom error page
            Server.ClearError();
            log.Info("Application_Error(): " + sb.ToString());
            Server.Transfer("ErrorOccurred.aspx", false);
        }

        protected void Session_End(object sender, EventArgs e)
        {
            log.Info("Session_End() Event :: " + e.ToString());
        }

        protected void Application_End(object sender, EventArgs e)
        {
            log.Info("Application_End() Event :: " + e.ToString());
        }
    }
}